package com.hellogood.yaohuo.server.mgr;

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;

import com.hellogood.yaohuo.server.DateUtil;
import com.hellogood.yaohuo.server.dao.factory.DAOFactory;
import com.hellogood.yaohuo.shared.entity.Log;
import com.hellogood.yaohuo.shared.entity.PsDetail;
import com.hellogood.yaohuo.shared.entity.PsForm;
import com.hellogood.yaohuo.shared.exception.DatabaseException;
import com.hellogood.yaohuo.shared.vo.MainFrame;

public class PeisongMgr {
	Logger log = Logger.getLogger(this.getClass());

	public String createPeisongForm(PsForm form) {
		execute(form);
		String result = DAOFactory.getInstance().createPsFormDAO().createPeisong(form, false);
		if (result.equals("")) {
			addLog(form.getPsBillNo(), false);
		}
		return result;
	}

	public String createPeisongForm(List<PsForm> list, Set<String> billNos) {
		String result = DAOFactory.getInstance().createPsFormDAO().createPeisongBatch(list, billNos);
		if (result.equals("")) {
			for (PsForm form : list) {
				addLog(form.getPsBillNo(), false);
			}
		}
		return result;
	}

	private void execute(PsForm form) {
		List<PsDetail> list = form.getList();
		int totalCounts = 0;
		BigDecimal formCost = new BigDecimal(0);
		BigDecimal formTotal = new BigDecimal(0);
		BigDecimal formPsCost = new BigDecimal(0);
		for (PsDetail d : list) {
			BigDecimal cost = d.getjPrice().multiply(new BigDecimal(d.getCounts()));
			BigDecimal total = d.getPrice().multiply(new BigDecimal(d.getCounts()));
			d.setNetCost(cost);
			d.setCost(cost);
			d.setTotal(total);
			totalCounts += d.getCounts();
			formCost = formCost.add(cost);
			formTotal = formTotal.add(total);
			formPsCost = formPsCost.add(d.getPsCost());
		}
		form.setPsCost(formPsCost);
		form.setCounts(totalCounts);
		form.setCost(formCost);
		form.setTotal(formTotal);
	}

	private void addLog(String billNo, boolean isModify) {
		int logID = DAOFactory.getInstance().createLogDAO().getLastLogNo();
		logID++;
		Log log = new Log();
		log.setLogId(logID);
		log.setStaffCode(MainFrame.currentUser.getStaffCode());
		log.setStaffName(MainFrame.currentUser.getStaffName());
		log.setLogDate(DateUtil.currentTime());
		if (isModify) {
			log.setContent("修改配送单：" + billNo);
		} else {
			log.setContent("新建配送单：" + billNo);
		}
		DAOFactory.getInstance().createLogDAO().addNewLog(log);
	}

	public String modifyPeisongForm(PsForm form) {
		execute(form);
		String result = DAOFactory.getInstance().createPsFormDAO().createPeisong(form, true);
		if (result.equals("")) {
			addLog(form.getPsBillNo(), true);
		}
		return result;
	}

	public int getLastPsBillNo(String billNo) {
		String no = DAOFactory.getInstance().createPsFormDAO().getLastPsBillNo(billNo);
		if (no != null) {
			// 序号为最后两位 格式为"WLYH组织号YYYYMMDDXX" XX为序号
			if (no.length() > 2) {
				billNo = no.substring(no.length() - 2, no.length());
				return Integer.valueOf(billNo);
			}
		}
		return 0;
	}

	public List<PsForm> searchPsForm(String code, int level, String startDate, String endDate, String rz) {
		if (code.equals("美季")) {
			level = 1;
		}
		if (level == 2) {
			return DAOFactory.getInstance().createPsFormDAO().searchPsFormByQuyuCode(code, startDate, endDate, rz);
		} else if (level == 3) {
			return DAOFactory.getInstance().createPsFormDAO().searchPsForm(code, startDate, endDate, rz);
		} else if (level == 1) {
			return DAOFactory.getInstance().createPsFormDAO().searchPsFormByQuyuCode(null, startDate, endDate, rz);
		}
		return null;
	}

	public PsForm getFormDetail(String billNo) {
		PsForm form = DAOFactory.getInstance().createPsFormDAO().getFormByBillNo(billNo);
		List<PsDetail> list = DAOFactory.getInstance().createPsDetailDAO().getDetailByBillNo(billNo);
		form.setList(list);
		return form;
	}

	public String rz(List<String> bills) {
		String result = "";
		try {
			for (String bill : bills) {
				DAOFactory.getInstance().createPsFormDAO().rz(bill);
			}
		} catch (DatabaseException e) {
			log.error(e.getMessage());
			e.printStackTrace();
			result = e.getMessage();
		}
		return result;
	}

	public String deletePeisong(String billNo) {
		return DAOFactory.getInstance().createPsFormDAO().deletePeisong(billNo);
	}
}
